package com.rp.system.controller;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.IAtom;
import com.jfinal.plugin.activerecord.Record;
import com.rp.common.utils.JsonUtil;
import com.rp.common.utils.RpUtil;
import com.rp.controller.RpController;
import com.rp.system.po.DataDic;
import com.rp.system.po.DataDicType;
import com.rp.system.service.DataDicService;

@Controller
@RequestMapping("/rp/system/dataDicMng/")
public class DataDicController extends RpController {

	@Autowired
	private DataDicService ddService;

	@RequestMapping("showDataDicGrid")
	public ModelAndView showDataDicGrid() {
		
		
		return forward("/rp/system/dataDicMng/dataDicGrid.jsp");
	}
	
	@ResponseBody
	@RequestMapping("getDataDicByTypeBh")
	public List<DataDic> getDataDicByTypeBh(String typeBh){
		return ddService.getDdDao().find("from DataDic where typeBh = ? order by sort asc", typeBh);
	}
	
	@ResponseBody
	@RequestMapping("addDataDicType")
	public String addDataDicType(String data){
		
		final Map<String, Object> ddt = JsonUtil.jsonToMap(data);
		
		Db.use("rpDb").tx(new IAtom() {
			@Override
			public boolean run() throws SQLException {
				
				ddt.put("id", RpUtil.createGuid());
				ddt.put("createDate", RpUtil.getCurrentDatetime());
				ddt.put("version", 0);
				boolean i = Db.use("rpDb").save("RP_SYSTEM_DATADICTYPES", new Record().setColumns(ddt));
				
				if(i){
					return true;
				}else{
					return false;
				}
			}
		});
		
		return SUCCESS;
	}
	
	@ResponseBody
	@RequestMapping("deleteDataDicType")
	public String deleteDataDicType(String typeId) {
		System.out.println(typeId);
		ddService.deleteDataDicType(typeId);
		return SUCCESS;
	}
	
	@ResponseBody
	@RequestMapping("saveDataDics")
	public String saveDataDics(String data){
		try {
			List<DataDic> dds = JsonUtil.jsonToList(data, "com.rp.system.po.DataDic");
			for (DataDic dd : dds) {
				if(dd.getId() == null){
					ddService.addDataDic(dd);
				}else{
					ddService.updateDateDic(dd);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
		
		return SUCCESS;
		
	}
	
	@ResponseBody
	@RequestMapping("deleteDataDics")
	public String deleteDataDics(final String data) {
		try {
			List<DataDic> dds = JsonUtil.jsonToList(data, "com.rp.system.po.DataDic");
			for (DataDic dd : dds) {
				ddService.deleteDataDic(dd);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
		
		return SUCCESS;
	}
	
	@ResponseBody
	@RequestMapping("getDataDicsByTypeBh")
	public List<DataDic> getDataDicsByTypeBh(String typeBh) {
		
		if(RpUtil.isNullOrEmpty(typeBh)){
			return ddService.getDdDao().find("from DataDic order by createDate desc");
		}else{
			return ddService.getDdDao().find("from DataDic where typeBh = ? order by createDate desc", typeBh);
		}
		
	}
	
	@ResponseBody
	@RequestMapping("getAllDataDicType")
	public List<DataDicType> getAllDataDicType() {
		
		return ddService.getDdtDao().find("from DataDicType order by createDate desc");
	}
	
}
